package com.hanbang.mrcm.merchant;

import java.util.ArrayList;
import java.util.Collection;

import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.AdapterView.OnItemClickListener;

import com.hanbang.mrcm.R;
import com.hanbang.mrcm.aboutme.AboutMeActivity;
import com.hanbang.mrcm.base.MainBaseActivity;
import com.hanbang.mrcm.bean.MerchantData2;
import com.hanbang.mrcm.home.GoodListActivity;
import com.hanbang.mrcm.home.HomeActivity;
import com.hanbang.mrcm.myview.ScaleImageView;
import com.hanbang.mrcm.utils.HttpInterfaces;
import com.hanbang.mrcm.utils.JsonHelper;
import com.hanbang.mrcm.utils.MyRequestCallBack;
import com.hanbang.mrcm.utils.SystemTools;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

public class MerchantActivity extends MainBaseActivity {
	private PullToRefreshGridView gridView = null;

	private ArrayList<MerchantData2> listDatas = new ArrayList<MerchantData2>();

	private MyAdapter adapter = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.merchant);
		bitmapUtils.configDefaultLoadFailedImage(R.drawable.default_img1_1);
		initView();
		setOnClickListener();
		getallSeller();
	}

	/*
	 * 设置点击事件
	 */
	private void setOnClickListener() {
		ViewGroup v1 = (ViewGroup) findViewById(R.id.bottom_bar_ll1);
		ViewGroup v2 = (ViewGroup) findViewById(R.id.bottom_bar_ll2);
		ViewGroup v3 = (ViewGroup) findViewById(R.id.bottom_bar_ll3);
		setBottonButton(v2);
		v1.setOnClickListener(onClickListener);
		v3.setOnClickListener(onClickListener);

		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent();
				intent.setClass(MerchantActivity.this, GoodListActivity.class);
				intent.putExtra("trade_id", listDatas.get(position)
						.getTrade_id());
				startActivity(intent);

			}
		});
	}

	private OnClickListener onClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			switch (v.getId()) {
			case R.id.bottom_bar_ll1:
				intent.setClass(MerchantActivity.this, HomeActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
				startActivity(intent);
				break;
			case R.id.bottom_bar_ll3:
				intent.setClass(MerchantActivity.this, AboutMeActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
				startActivity(intent);
				break;
			default:
				break;
			}
		}
	};

	/*
	 * 初始化view
	 */
	private void initView() {
		// 设置顶部的标题
		setTitle(findViewById(R.id.top_bar_title),
				getString(R.string.botton_tab2));
		gridView = (PullToRefreshGridView) findViewById(R.id.gridview);
		gridView.setMode(Mode.DISABLED);
		adapter = new MyAdapter();
		gridView.setAdapter(adapter);

	}

	private class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return listDatas.size();
		}

		@Override
		public Object getItem(int position) {
			return listDatas.get(position);
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@SuppressLint("InflateParams")
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			MerchantData2 data = (MerchantData2) getItem(position);
			ViewHolder holder = null;
			if (convertView == null) {
				convertView = getLayoutInflater().inflate(
						R.layout.merchant_list_item, null);
				holder = new ViewHolder();
				holder.imageView = (ScaleImageView) convertView
						.findViewById(R.id.imageview);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			bitmapUtils.display(holder.imageView,
					SystemTools.getHttpFileUrl(data.getSeller_logo()));
			return convertView;
		}

		private class ViewHolder {
			ScaleImageView imageView = null;
		}
	}

	private void getallSeller() {

		httpUtils.send(HttpMethod.GET, HttpInterfaces.allSeller,
				new MyRequestCallBack<String>(this,
						getString(R.string.loadding)) {
					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						super.onSuccess(responseInfo);
						try {
							JSONObject json = new JSONObject(
									responseInfo.result);
							int status = json.getInt("status");
							if (status == 1) {
								Collection<MerchantData2> c = JsonHelper
										.parseCollection(
												json.getJSONArray("result"),
												ArrayList.class,
												MerchantData2.class);
								listDatas.addAll(c);
								adapter.notifyDataSetChanged();
							}

						} catch (JSONException e) {
							e.printStackTrace();
						}

					}
				});
	}

	@Override
	protected void onResume() {
		bitmapUtils.configDefaultLoadFailedImage(R.drawable.default_img1_1);
		super.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
}
